﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Configuration;

namespace IHISCETeim.DAL
{
    public class ReMachineTestDAL
    {
        /// <summary>
        /// 获取所有的模板数据
        /// </summary>
        /// <returns></returns>
        public static IDataReader ReMachineTestTemplete_GetAll()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTestTemplete_GetAll");
            return DB.ExecuteReader(dbCommand);
        }

        public static int Add(ReMachineTestInfo obj,List<ReMachineTestDetailInfo> objDetail)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    int TestID = 0;
                    DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTest_Add");
                    DB.AddInParameter(dbCommand, "MachineType", DbType.String, obj.MachineType);
                    DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
                    DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
                    DB.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, obj.CreateDate);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

                    //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                    TestID= Convert.ToInt32(DB.ExecuteScalar(dbCommand,trans));

                    foreach (ReMachineTestDetailInfo itm in objDetail)
                    {
                        DbCommand dbCommandParts = DB.GetStoredProcCommand("ReMachineTest_AddDetail", TestID,itm.TemID,itm.CheckResult);
                        DB.ExecuteScalar(dbCommandParts, trans);
                    }

                  
                    trans.Commit();
                    return TestID;
                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "ReMachineTest_Add.Add", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
              
            }

        }

        public static bool Delete(int TestID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTest_Delete", TestID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetDetailByTestID(int TestID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTest_GetDetailByTestID", TestID);
            return DB.ExecuteReader(dbCommand);
        }
        public static IDataReader GetList(int TestID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTest_GetList", TestID);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTest_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static bool Update(ReMachineTestInfo obj, List<ReMachineTestDetailInfo> objDetail)
        {
            //Database DB = DatabaseFactory.CreateDatabase();

            //DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTest_Update");
            //DB.AddInParameter(dbCommand, "TestID", DbType.Int32, obj.TestID);
            //DB.AddInParameter(dbCommand, "MachineType", DbType.String, obj.MachineType);
            //DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
            //DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
            //DB.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, obj.CreateDate);
            //DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

            //try
            //{
            //    DB.ExecuteScalar(dbCommand);
            //    return true;
            //}
            //catch (Exception exc)
            //{
            //    return false;
            //}


            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                   // int TestID = 0;
                    DbCommand dbCommand = DB.GetStoredProcCommand("ReMachineTest_Update");
                    DB.AddInParameter(dbCommand, "TestID", DbType.Int32, obj.TestID);
                    DB.AddInParameter(dbCommand, "MachineType", DbType.String, obj.MachineType);
                    DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
                    DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
                    DB.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, obj.CreateDate);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

                    //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                    DB.ExecuteScalar(dbCommand,trans);

                    foreach (ReMachineTestDetailInfo itm in objDetail)
                    {
                        DbCommand dbCommandParts = DB.GetStoredProcCommand("ReMachineTest_AddDetail", obj.TestID, itm.TemID, itm.CheckResult);
                        DB.ExecuteScalar(dbCommandParts, trans);
                    }


                    trans.Commit();
                    return true;
                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "ReMachineTest_Update.Add", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return false;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }

            }

        }
    }
}
